﻿#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AlphaMobileControls;
using gpsMe.Classes;
using gpsMe.Properties;
using gpsMe.Utils;
#endregion

namespace gpsMe.UI.Forms
{
    /// <summary>
    /// Main menu of gpsMe
    /// </summary>
    public partial class gpsMeMainMenuForm : AlphaForm
    {
        #region Instance
        /// <summary>
        /// Current gpsMeMainMenuForm Instance
        /// </summary>
        public static gpsMeMainMenuForm Instance { get; private set; }
        #endregion

        #region Controls
        /// <summary>
        /// PictureBox of the background image
        /// </summary>
        private AlphaPictureBox backgroundPictureBox;
        /// <summary>
        /// Button that launches the navigation
        /// </summary>
        private AlphaImageButton launchNavigationButton;
        /// <summary>
        /// Button that launches the change map form
        /// </summary>
        private AlphaImageButton changeMapButton;
        /// <summary>
        /// Button that launches the extra tools form
        /// </summary>
        private AlphaImageButton extraToolsButton;
        /// <summary>
        /// Button that launches the settings form
        /// </summary>
        private AlphaImageButton settingsButton;
        /// <summary>
        /// Button that launches the about form
        /// </summary>
        private AlphaImageButton aboutButton;
        /// <summary>
        /// Button that exit gpsMe
        /// </summary>
        private AlphaImageButton exitButton;
        #endregion

        #region Constructor
        /// <summary>
        /// Default Constructor
        /// </summary>
        public gpsMeMainMenuForm()
        {
            Instance = this;
            InitializeComponent();
            this.Text = gpsMeProperties.GPSME_TITLE;
            this.Resize += new EventHandler(gpsMeMainMenuForm_Resize);
            gpsMeMainForm.Instance.Hide();
            this.SuspendLayout();

            #region Controls initialization
            this.backgroundPictureBox = new AlphaPictureBox();
            this.launchNavigationButton = new AlphaImageButton();
            this.changeMapButton = new AlphaImageButton();
            this.extraToolsButton = new AlphaImageButton();
            this.settingsButton = new AlphaImageButton();
            this.aboutButton = new AlphaImageButton();
            this.exitButton = new AlphaImageButton();
            this.backgroundPictureBox.Image = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\mainMenuBackground.png");
            this.launchNavigationButton.DisabledBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\navigateIcon.png");
            this.launchNavigationButton.ActiveBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\navigateIcon_pressed.png");
            this.launchNavigationButton.BackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\navigateIcon.png");
            this.changeMapButton.DisabledBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\chooseMapIcon.png");
            this.changeMapButton.ActiveBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\chooseMapIcon_pressed.png");
            this.changeMapButton.BackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\chooseMapIcon.png");
            this.extraToolsButton.DisabledBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\extraToolsIcon.png");
            this.extraToolsButton.ActiveBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\extraToolsIcon_pressed.png");
            this.extraToolsButton.BackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\extraToolsIcon.png");
            this.aboutButton.DisabledBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\aboutIcon.png");
            this.aboutButton.ActiveBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\aboutIcon_pressed.png");
            this.aboutButton.BackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\aboutIcon.png");
            this.settingsButton.DisabledBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\settingsIcon.png");
            this.settingsButton.ActiveBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\settingsIcon_pressed.png");
            this.settingsButton.BackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\settingsIcon.png");
            this.exitButton.DisabledBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\exitIcon.png");
            this.exitButton.ActiveBackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\exitIcon_pressed.png");
            this.exitButton.BackgroundImage = AlphaImage.CreateFromFile(gpsMeUtils.ApplicationDirectory + @"\Images\exitIcon.png");
            this.backgroundPictureBox.Size = new Size(gpsMeMainMenuFormProperties.MAIN_MENU_BACKGROUND_WIDTH, gpsMeMainMenuFormProperties.MAIN_MENU_BACKGROUND_HEIGHT);
            Size oButtonsSize = new Size(gpsMeMainMenuFormProperties.MAIN_MENU_BUTTON_WIDTH, gpsMeMainMenuFormProperties.MAIN_MENU_BUTTON_HEIGHT);
            this.launchNavigationButton.Size = oButtonsSize;
            this.changeMapButton.Size = oButtonsSize;
            this.extraToolsButton.Size = oButtonsSize;
            this.aboutButton.Size = oButtonsSize;
            this.settingsButton.Size = oButtonsSize;
            this.exitButton.Size = oButtonsSize;
            #endregion

            #region Controls association
            this.Controls.Add(this.launchNavigationButton);
            this.Controls.Add(this.changeMapButton);
            this.Controls.Add(this.extraToolsButton);
            this.Controls.Add(this.aboutButton);
            this.Controls.Add(this.settingsButton);
            this.Controls.Add(this.exitButton);
            this.Controls.Add(this.backgroundPictureBox);
            #endregion

            #region Action association
            this.launchNavigationButton.Click += new EventHandler(launchNavigationButton_Click);
            this.changeMapButton.Click += new EventHandler(changeMapButton_Click);
            this.extraToolsButton.Click += new EventHandler(extraToolsButton_Click);
            this.aboutButton.Click += new EventHandler(aboutButton_Click);
            this.settingsButton.Click += new EventHandler(settingsButton_Click);
            this.exitButton.Click += new EventHandler(exitButton_Click);
            #endregion

            this.ResumeLayout();
        }
        #endregion

        #region Functions / Methods / Events
        /// <summary>
        /// Action performed when the exit button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void exitButton_Click(object sender, EventArgs e)
        {
            DialogResult oDlgResult = MessageBox.Show(gpsMeLanguage.Instance.ConfirmExitLabel, gpsMeLanguage.Instance.WarningLabel, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            if (oDlgResult == DialogResult.Yes)
                gpsMeMainForm.Instance.Close();
        }

        /// <summary>
        /// Action performed when the settings button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void settingsButton_Click(object sender, EventArgs e)
        {
            if ((gpsMeSettingsForm.Instance == null) || gpsMeSettingsForm.Instance.IsDisposed)
                new gpsMeSettingsForm();
            gpsMeSettingsForm.Instance.ShowDialog();
            gpsMeSettingsForm.Instance.Dispose();
        }

        /// <summary>
        /// Action performed when the about button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void aboutButton_Click(object sender, EventArgs e)
        {
            if ((gpsMeAboutForm.Instance == null) || gpsMeAboutForm.Instance.IsDisposed)
                new gpsMeAboutForm();
            gpsMeAboutForm.Instance.ShowDialog();
            gpsMeAboutForm.Instance.Dispose();
        }

        /// <summary>
        /// Action performed when the extraTools button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void extraToolsButton_Click(object sender, EventArgs e)
        {
            if ((gpsMeExtraToolsForm.Instance == null) || gpsMeExtraToolsForm.Instance.IsDisposed)
                new gpsMeExtraToolsForm();
            gpsMeExtraToolsForm.Instance.ShowDialog();
            gpsMeExtraToolsForm.Instance.Dispose();
        }

        /// <summary>
        /// Action performed when the changeMap button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void changeMapButton_Click(object sender, EventArgs e)
        {
            if ((gpsMeMapsForm.Instance == null) || gpsMeMapsForm.Instance.IsDisposed)
                new gpsMeMapsForm();
            gpsMeMapsForm.Instance.ShowDialog();
            gpsMeMapsForm.Instance.Dispose();
        }

        /// <summary>
        /// Action performed when the launchNavigation button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void launchNavigationButton_Click(object sender, EventArgs e)
        {
            while (!gpsMeMap.Instance.MapLoaded)
            {
                MessageBox.Show(gpsMeLanguage.Instance.ErrorNoMapSelectedField);
                if ((gpsMeMapsForm.Instance == null) || gpsMeMapsForm.Instance.IsDisposed)
                    new gpsMeMapsForm();
                gpsMeMapsForm.Instance.ShowDialog();
                gpsMeMapsForm.Instance.Dispose();
            }
            if ((gpsMeNavigationForm.Instance == null) || gpsMeNavigationForm.Instance.IsDisposed)
                new gpsMeNavigationForm();
            gpsMeNavigationForm.Instance.Show();
        }

        /// <summary>
        /// Action performed when the form is resized
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void gpsMeMainMenuForm_Resize(object sender, EventArgs e)
        {
            if (this.Height > this.Width)
            {
                this.backgroundPictureBox.Location = new Point((this.Width / 2) - (gpsMeMainMenuFormProperties.MAIN_MENU_BACKGROUND_WIDTH / 2), 0);
                int iLeftMargin = (this.Width / 4) - (gpsMeMainMenuFormProperties.MAIN_MENU_BUTTON_WIDTH / 2);
                int iTopMargin = (this.Height / 6) - (gpsMeMainMenuFormProperties.MAIN_MENU_BUTTON_HEIGHT / 2);
                int iCaseWidth = (this.Width / 2);
                int iCaseHeight = (this.Height / 3);
                this.launchNavigationButton.Location = new Point(iLeftMargin, iTopMargin);
                this.changeMapButton.Location = new Point(iCaseWidth + iLeftMargin, iTopMargin);
                this.extraToolsButton.Location = new Point(iLeftMargin, iCaseHeight + iTopMargin);
                this.aboutButton.Location = new Point(iCaseWidth + iLeftMargin, iCaseHeight + iTopMargin);
                this.settingsButton.Location = new Point(iLeftMargin, (iCaseHeight * 2) + iTopMargin);
                this.exitButton.Location = new Point(iCaseWidth + iLeftMargin, (iCaseHeight * 2) + iTopMargin);
            }
            else
            {
                this.backgroundPictureBox.Location = new Point(0, (this.Height / 2) - (gpsMeMainMenuFormProperties.MAIN_MENU_BACKGROUND_HEIGHT / 2));
                int iLeftMargin = (this.Width / 6) - (gpsMeMainMenuFormProperties.MAIN_MENU_BUTTON_WIDTH / 2);
                int iTopMargin = (this.Height / 4) - (gpsMeMainMenuFormProperties.MAIN_MENU_BUTTON_HEIGHT / 2);
                int iCaseWidth = (this.Width / 3);
                int iCaseHeight = (this.Height / 2);
                this.launchNavigationButton.Location = new Point(iLeftMargin, iTopMargin);
                this.changeMapButton.Location = new Point(iCaseWidth + iLeftMargin, iTopMargin);
                this.extraToolsButton.Location = new Point((iCaseWidth * 2) + iLeftMargin, iTopMargin);
                this.aboutButton.Location = new Point(iLeftMargin, iCaseHeight + iTopMargin);
                this.settingsButton.Location = new Point(iCaseWidth + iLeftMargin, iCaseHeight + iTopMargin);
                this.exitButton.Location = new Point((iCaseWidth * 2) + iLeftMargin, iCaseHeight + iTopMargin);
            }
        }
        #endregion
    }
}